/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
// 性质：左子树均小于它的根结点的值；右子树均大于它的根结点的值

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        TreeNode* cur = root;
        while(1){
            if(cur->val > p->val && cur->val > q->val)
                cur = cur->left; // 说明p和q应该在当前节点的左子树
            else if(cur->val < p->val && cur->val < q->val)
                cur = cur->right; // 说明p和q应该在当前节点的右子树
            else break;   // 满足条件
        }
        return cur;
    }
};